;
(function() {
  new Vue({
    el: '#project-file',
    data: {
      projectId: hlUrlUtil.GetQueryString('projectId'),
      fileList: [],
      progress: '',
    },
    methods: {
      prohectDt() {
        hlhttp.get({
          url: 'project',
          data: {
            project_id: this.projectId
          },
          success: (data) => {
            if (data.attachmentNames) {
              let fileNames = data.attachmentNames.split(',');
              let fileUrls = data.attachmentUrls.split(',');
              fileNames.map((item, index) => {
                this.fileList.push({
                  name: item,
                  url: fileUrls[index]
                })
              })
            }
          }
        })
      },
      fileUpEvent() {
        if (this.progress) {
          hl.toast('文件尚未上传完成')
          return
        }
        document.querySelector('.file-up').click()
      },
      //上传文件
      upfile(data) {
        hlhttp.post({
          url: 'project_attachment',
          data: {
            project_id: this.projectId,
            attachment_names: data.name,
            attachment_url: data.url
          },
          success: () => {
            this.fileList.push({
              name: data.name,
              url: data.url
            })
          }
        })
      },
      //删除文件
      delFile(i, data) {
        hlhttp.delete({
          url: 'project_attachment?project_id=' + this.projectId + '&' + 'attachment_url=' + data.url,
          success: () => {
            this.fileList.splice(i, 1)
          }
        })
      },
      previewImage:function(url){
        var imageExt = ['jpg', 'jpeg', 'png', 'JPG', 'JPEG', 'PNG'];
        var pdfExt = ['pdf', 'PDF'];
        var officeExt = ['docx','doc','xlsx','xls','xlt']
        var imageFlag = false;
        var pdfFlag = false;
        var officeFlag = false;

        imageExt.forEach((e, i) => {
          if (url.indexOf(e) !== -1) imageFlag = true;
        })

        if (imageFlag) {
          $.imageShow([url])
          return
        }
        pdfExt.forEach((e, i) => {
          if (url.indexOf(e) !== -1) pdfFlag = true;
        })
        if (pdfFlag) {
          $.previewPDF(url)
          return
        }

        officeExt.forEach((e, i) => {
          if (url.indexOf(e) !== -1) officeFlag = true
        })
        if (officeFlag) {
          $.previewOffice(url)
          return
        }
        hl.toast('暂不支持该格式')
      },
    },
    mounted() {
      this.prohectDt()
      var that = this
      fileUpload('.file-up', uploadUrl, (data) => {
        that.progress = ''
        this.upfile(data.json)

      }, function(val) {
        that.progress = parseInt((val.loaded / val.total) * 100) + '%'
      })
    }
  })
}())
